package ro.prjmanager.web.actions.synch;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.Resolution;
import ro.prjmanager.model.*;
import ro.prjmanager.service.SynchInterfaceService;
import ro.prjmanager.core.usr.UserSession;
import ro.prjmanager.web.actions.GeneralAction;
import ro.prjmanager.web.annotation.Service;

import java.util.ArrayList;
import java.util.List;

import static ro.prjmanager.service.SynchInterfaceService.MYSQL;
import static ro.prjmanager.service.SynchInterfaceService.ORACLE;

@Service(target = "synchService")
@SuppressWarnings({"unchecked"})
public class SynchAction extends GeneralAction {
    private List<TableSet> tableList;
    private List<String> tableIds;
    private boolean synchronizeAllTableSet;

    public List<String> getTableIds() {
        return tableIds;
    }

    public void setTableIds(List<String> tableIds) {
        this.tableIds = tableIds;
    }

    public List<TableSet> getTableList() {
        return tableList;
    }

    public void setTableList(List<TableSet> tableList) {
        this.tableList = tableList;
    }

    public boolean isSynchronizeAllTableSet() {
        return synchronizeAllTableSet;
    }

    public void setSynchronizeAllTableSet(boolean synchronizeAllTableSet) {
        this.synchronizeAllTableSet = synchronizeAllTableSet;
    }

    @DefaultHandler
    @HandlesEvent("showSynch")
    public Resolution loadForSynch() {
        SynchInterfaceService synchService = (SynchInterfaceService) getService();


        List<TableSet> tableSetList = getService("tablesetService").list();
        if (tableSetList != null && !tableSetList.isEmpty()) {
            setTableList(new ArrayList<TableSet>());
            for (TableSet tableSet : tableSetList) {
                    getTableList().add(tableSet);
            }
        }





        return new ForwardResolution("/pages/nomenclators/synch/physicLogicSynchronizer.jsp");
    }

    @HandlesEvent("sync")
    public Resolution synch() {
        SynchInterfaceService synchService = (SynchInterfaceService) getService();
        UserSession<Users, Projects, Svn, DataBases,Schema> userSession = getContext().getUserSession();

        int type = (userSession.getDataBases().getFkDbType().getName().
                equalsIgnoreCase("oracle")) ? ORACLE : MYSQL;
        List<TableSet> tableSetList = null;
        if (synchronizeAllTableSet) {
            tableSetList = getService("tablesetService").list();

        } else {

            if (tableIds != null && !tableIds.isEmpty()) {
                StringBuilder tableNames = new StringBuilder();
                for (String str : tableIds) {
                    tableNames.append(str.toUpperCase()).append(",");
                }
                tableNames.append("-1");
                tableSetList = getService("tablesetService").listPagFilter
                        (0, tableIds.size(), "where upper(c.id) in(" + tableNames + ")");
            }

        }

        String url = type==MYSQL?userSession.getDataBases().getUrl() + userSession.getSchema().getName()
                :userSession.getDataBases().getUrl();
        synchService.syncTable(url,
                userSession.getSchema().getUser(),
                userSession.getSchema().getPassword(),
                userSession.getSchema().getUser()
                , type, tableSetList);         

        return loadForSynch();
    }


}
